﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using SystemUpdatesCore.Server;
using AppUpdates.Core;
using System.Runtime.ConstrainedExecution;

using System;
using System.IO;
using System.IO.Compression;

namespace SystemUpdatesCore
{
    public class Zip : CoreBase
    {
        public Zip()
        {

        }

        public static Task<List<string>> ExtractToDirectoryTaskAsync(List<string> files, string? directory = null)
        {
            return Task.Factory.StartNew((state) =>
            {
                var o = state as dynamic;

                List<string> paths = new List<string>();
                foreach (string file in o.files)
                {
                    var path = Path.Combine((o.directory ?? FilePath), file.Replace(".zip", "", true, null));
                    var filePath = Path.Combine(FilePath, file);
                    ExtractToDirectory(path, filePath);

                    paths.Add(path);
                }

                return paths;

            }, new { files, directory });
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="zipPath"></param>
        public static void CreateFromDirectory(string folderPath, string zipPath)
        {
            // 压缩 
            System.IO.Compression.ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Fastest, false, Encoding.UTF8);
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="zipPath"></param>
        public static void ExtractToDirectory(string folderPath, string zipPath)
        {
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            //解压
            System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, folderPath, Encoding.UTF8, true);
        }
    }
}
